package com.daiyang.lottery.common.intercept;

import com.daiyang.lottery.common.util.JWTUtil;
import com.daiyang.lottery.controller.UserContoller;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
public class LoginInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request
            , HttpServletResponse response, Object handler) throws Exception {
        String token =request.getHeader("user_token");
        logger.info("获取路径:{}", request.getRequestURI());
        logger.info("从header中获取token:{}",token);
        //验证用户 token
        Claims claims = JWTUtil.parseJWT(token);
        if(claims==null) {
            response.setStatus(401);
            return true;
        }
        logger.info("令牌通过，放行");
        return true;
    }
}
